package com.bw.yk08;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.runtime.state.StateBackend;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

public class Test03 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env=StreamExecutionEnvironment.createLocalEnvironment();
        env.setParallelism(1);


        DataStreamSource<String> odsSourceDs=env.addSource(MyKafkaUtil.getFlinkKafkaConsumer("ods_base_topic","test03"));

//        odsSourceDs.print();

        SingleOutputStreamOperator<String> odsDs=odsSourceDs.filter(
                new FilterFunction<String>() {
                    @Override
                    public boolean filter(String s) throws Exception {
                        try
                        {
                            JSONObject ob= JSON.parseObject(s);
                            String create_time=ob.getJSONObject("data").getString("create_time");
                            if(create_time!=null&&create_time.length()==19)
                            {
                                return true;
                            }
                        }catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                        return false;
                    }
                }
        );


        OutputTag<String> order_detail=new OutputTag<String>("dim_table_source"){};

        SingleOutputStreamOperator<String> orderDS=odsDs.process(
                new ProcessFunction<String, String>() {

                    @Override
                    public void processElement(String s, ProcessFunction<String, String>.Context context, Collector<String> collector) throws Exception {
                        JSONObject ob= JSON.parseObject(s);

                        if(ob!=null)
                        {
                            String table=ob.getString("table");
                            String dim_tables="region,shop,product_sku,product_spu";
                            if(dim_tables.contains(table))
                            {
                                context.output(order_detail,ob.toJSONString());
                            }else
                                collector.collect(ob.toJSONString());
                        }
                    }
                }
        );

        orderDS.addSink(new HbaseSinkFunc());

        env.execute();
    }
}
